from flask import Blueprint,jsonify,request
from utils.comm import get_data,xtree
from db import db1
import datetime



bp_organization = Blueprint('organization',__name__)


@bp_organization.route('/organizationlist',methods=['GET','POST'])
def organizationlist():
    sql = "select id,name,pid from organization "
    res = db1.find_all(sql)
    return jsonify({'code':200,'list':res})



@bp_organization.route('/deptlist',methods=['GET','POST'])
def deptlist():
    if request.method == 'POST':
        data = request.json

        sql = "select id from dept where coding='%s'"% data['coding']
        res = db1.find(sql)
        time = datetime.datetime.strftime(datetime.datetime.now(), '%y-%m-%d')
        sql1 = "insert into dept (name,coding,pid,starttime) values ('%s','%s',%d,'%s')"%(data['name'],data['code'],int(res['id']),time)
        db1.update(sql1)
        return jsonify({'code':200,'msg':'ok'})

    else:
        sql = "select id,name,coding,pid from dept"
        res = db1.find_all(sql)
        data = xtree(res)
        return jsonify({'code': 200, 'list': data})


@bp_organization.route('/getposts',methods=['POST','GET'])
def getposts():
    if request.method == 'POST':
        data = request.json
        print(data)
        sql = "update posts set name='%s',describe1='%s',dept_id='%s' where id=%d"%(data['name'],data['describe'],data['dept_id'],int(data['pid']))
        db1.update(sql)
        return jsonify({'code':200})
    else:
        test = request.args.get('test')
        if test:
            sql = "select * from posts where name like '%s'" % ("%"+test+"%")
        else:
            sql = "select * from posts"
        res = db1.find_all(sql)
        return jsonify({'code': 200, 'data': res})


@bp_organization.route('/addposts',methods=['POST','DELETE'])
def addposts():
    if request.method == 'POST':
        data = request.json
        sql = "insert into posts (name,describe1,createuser,dept_id) value('%s','%s','%s','%s')"%(data['name'],data['describe'],data['createuser'],data['dept_id'])
        db1.update(sql)
        return jsonify({'code':200})
    else:
        id = request.args.get('id')
        sql = "delete from posts where id=%d"%int(id)
        db1.update(sql)
        return jsonify({'code':201})



@bp_organization.route('/getdutys',methods=['POST','GET'])
def getdutys():
    if request.method == 'POST':
        data = request.json
        sql = "update dutys set name='%s',coding='%s' where id=%d"%(data['name'],data['coding'],int(data['did']))
        db1.update(sql)
        return jsonify({'code':200})
    else:
        test = request.args.get('test')
        if test:
            sql = "select * from dutys where name like '%s'" % ("%"+test+"%")
        else:
            sql = "select * from dutys"
        res = db1.find_all(sql)
        return jsonify({'code': 200, 'data': res})

@bp_organization.route('/adddutys',methods=['POST','DELETE'])
def adddutys():
    if request.method == 'POST':
        data = request.json
        sql = "insert into dutys (name,coding) value('%s','%s')"%(data['name'],data['coding'])
        db1.update(sql)
        return jsonify({'code':200})
    else:
        id = request.args.get('id')
        sql = "delete from dutys where id=%d"%int(id)
        db1.update(sql)
        return jsonify({'code':201})



@bp_organization.route('/getusers',methods=['POST','GET'])
def getusers():
    if request.method == 'POST':
        data = request.json
        sql = "update users set name='%s',gender='%s',mobile='%s' where id=%d"%(data['name'],data['gender'],data['mobile'],int(data['id']))
        db1.update(sql)
        return jsonify({'code':200})
    else:
        test = request.args.get('test')
        coding = request.args.get('coding')
        if test:
            sql = "select * from users where name like '%s'" % ("%"+test+"%")
        else:
            sql1 = "select * from dept where coding='%s'"% coding
            re1 = db1.find(sql1)
            sql = "select users.id,users.name,users.gender,users.mobile,users.emit,users.coding from " \
                  "users inner join dept on dept_id=dept.id where dept_id=%d or pid=%d"%(int(re1['id']),int(re1['id']))
        res = db1.find_all(sql)
        return jsonify({'code': 200, 'data': res})



@bp_organization.route('/resource')
def resource():
    user_id = request.args.get('user_id')
    if user_id:
        sql = "select resource.id,resource.name,resource.url from  user_res inner join " \
              "resource on user_res.resource_id=resource.id where user_res.user_id=%d and resource.type=1"% int(user_id)
    else:
        sql = "select resource.id,resource.name,resource.url from  resource  where  resource.type=1"
    res = db1.find_all(sql)
    return jsonify({'code': 200, 'list': res})